home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 285_02 / print.c < prev    next >
Text File  |  1990-07-08  |  5KB  |  236 lines

  1. /* Print information on generated parser, for bison,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. #include <stdio.h>
  22. #include "machine.h"
  23. #include "new.h"
  24. #include "files.h"
  25. #include "gram.h"
  26. #include "state.h"
  27. #include "conflict.h"
  28. #include "print.h"
  29.  
  30. extern char **tags;
  31. extern int nstates;
  32. extern short *accessing_symbol;
  33. extern core **state_table;
  34. extern shifts **shift_table;
  35. extern errs **err_table;
  36. extern reductions **reduction_table;
  37. extern char *consistent;
  38. extern char any_conflicts;
  39. extern char *conflicts;
  40.  
  41.  
  42.  
  43. void terse()
  44. {
  45.   if (any_conflicts)
  46.     {
  47.       conflict_log();
  48.     }
  49. }
  50.  
  51.  
  52.  
  53. void verbose()
  54. {
  55.   register int i;
  56.  
  57.   if (any_conflicts)
  58.     verbose_conflict_log();
  59.  
  60.   fprintf(foutput, "\n\ntoken types:\n");
  61.   print_token (-1, 0);
  62.   if (translations)
  63.     {
  64.       for (i = 0; i <= max_user_token_number; i++)
  65.     /* Don't mention all the meaningless ones.  */
  66.     if (token_translations[i] != 2)
  67.       print_token (i, token_translations[i]);
  68.     }
  69.   else
  70.     for (i = 1; i < ntokens; i++)
  71.       print_token (i, i);
  72.  
  73.   for (i = 0; i < nstates; i++)
  74.     {
  75.       print_state(i);
  76.     }
  77. }
  78.  
  79.  
  80.  
  81. void print_token(extnum, token)
  82. int extnum, token;
  83. {
  84.   fprintf(foutput, " type %d is %s\n", extnum, tags[token]);
  85. }
  86.  
  87.  
  88.  
  89. void print_state(state)
  90. int state;
  91. {
  92.   fprintf(foutput, "\n\nstate %d\n\n", state);
  93.   print_core(state);
  94.   print_actions(state);
  95. }
  96.  
  97.  
  98.  
  99. void print_core(state)
  100. int state;
  101. {
  102.   register int i;
  103.   register int k;
  104.   register int rule;
  105.   register core *statep;
  106.   register short *sp;
  107.   register short *sp1;
  108.  
  109.   statep = state_table[state];
  110.   k = statep->nitems;
  111.  
  112.   if (k == 0) return;
  113.  
  114.   for (i = 0; i < k; i++)
  115.     {
  116.       sp1 = sp = ritem + statep->items[i];
  117.  
  118.       while (*sp > 0)
  119.     sp++;
  120.  
  121.       rule = -(*sp);
  122.       fprintf(foutput, "    %s  ->  ", tags[rlhs[rule]]);
  123.  
  124.       for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
  125.     {
  126.       fprintf(foutput, "%s ", tags[*sp]);
  127.     }
  128.  
  129.       putc('.', foutput);
  130.  
  131.       while (*sp > 0)
  132.     {
  133.       fprintf(foutput, " %s", tags[*sp]);
  134.       sp++;
  135.     }
  136.  
  137.       fprintf (foutput, "   (%d)", rule);
  138.       putc('\n', foutput);
  139.     }
  140.  
  141.   putc('\n', foutput);
  142. }
  143.  
  144.  
  145.  
  146. void print_actions(state)
  147. int state;
  148. {
  149.   register int i;
  150.   register int k;
  151.   register int state1;
  152.   register int symbol;
  153.   register shifts *shiftp;
  154.   register errs *errp;
  155.   register reductions *redp;
  156.   register int rule;
  157.  
  158.   shiftp = shift_table[state];
  159.   redp = reduction_table[state];
  160.   errp = err_table[state];
  161.  
  162.   if (!shiftp && !redp)
  163.     {
  164.       fprintf(foutput, "    NO ACTIONS\n");
  165.       return;
  166.     }
  167.  
  168.   if (shiftp)
  169.     {
  170.       k = shiftp->nshifts;
  171.  
  172.       for (i = 0; i < k; i++)
  173.     {
  174.       if (! shiftp->shifts[i]) continue;
  175.       state1 = shiftp->shifts[i];
  176.       symbol = accessing_symbol[state1];
  177. /*    if (ISVAR(symbol)) break;  */
  178.       fprintf(foutput, "    %-4s\tshift  %d\n", tags[symbol], state1);
  179.     }
  180.  
  181.       if (i > 0)
  182.     putc('\n', foutput);
  183.     }
  184.   else
  185.     {
  186.       i = 0;
  187.       k = 0;
  188.     }
  189.  
  190.   if (errp)
  191.     {
  192.       k = errp->nerrs;
  193.  
  194.       for (i = 0; i < k; i++)
  195.     {
  196.       if (! errp->errs[i]) continue;
  197.       symbol = errp->errs[i];
  198.       fprintf(foutput, "    %-4s\terror (nonassociative)\n", tags[symbol]);
  199.     }
  200.  
  201.       if (i > 0)
  202.     putc('\n', foutput);
  203.     }
  204.   else
  205.     {
  206.       i = 0;
  207.       k = 0;
  208.     }
  209.  
  210.   if (consistent[state] && redp)
  211.     {
  212.       rule = redp->rules[0];
  213.       symbol = rlhs[rule];
  214.       fprintf(foutput, "    $default\treduce  %d  (%s)\n\n",
  215.                 rule, tags[symbol]);
  216.     }
  217.   else if (redp)
  218.     {
  219.       print_reductions(state);
  220.     }
  221.  
  222.   if (i < k)
  223.     {
  224.       for (; i < k; i++)
  225.     {
  226.       if (! shiftp->shifts[i]) continue;
  227.       state1 = shiftp->shifts[i];
  228.       symbol = accessing_symbol[state1];
  229.       fprintf(foutput, "    %-4s\tgoto  %d\n", tags[symbol], state1);
  230.     }
  231.  
  232.       putc('\n', foutput);
  233.     }
  234. }
  235.  
  236.